package cn.bocweb.spinning.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.CollectCompanyBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.adapter.CollectCompanyAdapter;
import cn.bocweb.spinning.view.adapter.CollectInfoAdapter;
import cn.bocweb.spinning.view.common.SwipeRefreshHelper;
import de.greenrobot.event.EventBus;

/**
 * Created by chenchangxing on 15/12/21.
 */
public class CollectCompanyFragment extends Fragment {

    CollectInfoAdapter adapter;
    List<CollectCompanyBean.DataBean> list;
    UserPresenter presenter;
    CollectCompanyAdapter collectCompanyAdapter;
    SwipeRefreshHelper swipeRefreshHelper;
    String user;
    int page = 1;
    boolean isLastPage;

    @Bind(R.id.listView)
    ListView listView;
    @Bind(R.id.company_empty)
    RelativeLayout companyEmpty;
    @Bind(R.id.refresh)
    SwipeRefreshLayout refresh;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_collect_company, container, false);
        ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        presenter = new UserPresenterImp();
        list = new ArrayList<>();
        collectCompanyAdapter = new CollectCompanyAdapter(getActivity(), list);


        UserBean.DataEntity dataEntity = new UserBean.DataEntity();
        dataEntity.readInfoFromLocal(getActivity());
        user = dataEntity.getUserid();

        initListView();

        presenter.getCollectCompany(user, String.valueOf(page));
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    private void initListView() {
        listView.setAdapter(collectCompanyAdapter);

        swipeRefreshHelper = new SwipeRefreshHelper(refresh, listView, new SwipeRefreshHelper.OnSwipeRefreshListener() {
            @Override
            public void onRefresh() {
                list.clear();
                page = 1;
                presenter.getCollectCompany(user, String.valueOf(page));
            }

            @Override
            public void onLoad() {
                if (!isLastPage) {
                    page++;
                    presenter.getCollectCompany(user, String.valueOf(page));
                } else {
                    DeviceUtil.MyToast(getActivity(), "已经是最后一页");
                }
            }
        });
    }

    public void onEventMainThread(CollectCompanyBean bean) {
        if (bean.getReturn_code() == 0) {
            list.addAll(bean.getData());
            collectCompanyAdapter.notifyDataSetChanged();
        } else if (bean.getReturn_code() == -202) {
            isLastPage = true;
            DeviceUtil.MyToast(getActivity(), "已经是最后一页");
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
